<?php

namespace App\Http\Controllers\Admin;

use App\Models\Shop;
use App\Models\ShopAuth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ShopController extends BaseController
{

    /**
     * 店铺列表
     * @throws \Exception
     */
    public function index(){

        $this->assign([
            'tab'=>'all',
            'shop_name'=>'',
            'username'=>'',
            'phone'=>'',
            'shop_status'=>'',
            'time_begin'=>'',
            'time_end'=>''
        ]);

        $queryParams = [];
        $query = Shop::with('user');

        $tab = $this->request->get('tab');
        if ($tab) {
            $queryParams['tab'] = $tab;
            if ($tab == 'open')
            {
                $query = $query->where('closed', 0);
            }
            if ($tab == 'closed')
            {
                $query = $query->where('closed', 1);
            }
        }

        $shop_name = $this->request->get('shop_name');
        if ($shop_name) {
            $queryParams['shop_name'] = $shop_name;
            $query = $query->where('shop_name', 'LIKE', "%$shop_name%");
        }

        $username = $this->request->get('username');
        if ($username) {
            $queryParams['username'] = $username;
            $query = $query->where('username', $username);
        }

        $phone = $this->request->get('phone');
        if ($phone) {
            $queryParams['phone'] = $phone;
            $query = $query->where('phone', $phone);
        }

        $shop_status = strtoupper($this->request->get('shop_status'));
        if ($shop_status) {
            $queryParams['shop_status'] = $shop_status;
            if ($shop_status == 'OPEN'){
                $query = $query->where('closed', 0);
            }
            if ($shop_status == 'CLOSE'){
                $query = $query->where('closed', 1);
            }
        }

        $time_begin = $this->request->get('time_begin');
        $time_end = $this->request->get('time_end');

        if ($time_begin) {
            $queryParams['time_begin'] = $time_begin;
            $query = $query->where('time_begin', '>', strtotime($time_begin));
        }

        if ($time_end) {
            $queryParams['time_end'] = $time_end;
            $query = $query->where('time_begin', '<', strtotime($time_end));
        }

        $this->assign($queryParams);

        $shops = $query->orderByDesc('shop_id')->paginate(20);
        $this->assign([
            'shops'=>$shops,
            'pagination'=>$shops->appends($queryParams)->render(),
        ]);

        return $this->view('admin.shop.list');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function batchDelete(Request $request)
    {
        $shops = $request->input('shops');
        if (is_array($shops))
        {
            Shop::whereIn('shop_id', $shops)->get()->map(function (Shop $shop){
                $shop->delete();
            });
        }

        return ajaxReturn();
    }

    public function batchClose(Request $request)
    {
        $shops = $request->input('shops');
        if (is_array($shops))
        {
            Shop::whereIn('shop_id', $shops)->update(['closed'=>1]);
        }

        return ajaxReturn();
    }

    public function batchOpen(Request $request)
    {
        $shops = $request->input('shops');
        if (is_array($shops))
        {
            Shop::whereIn('shop_id', $shops)->update(['closed'=>0]);
        }

        return ajaxReturn();
    }

    /**
     * 等待审核
     * @throws \Exception
     */
    public function pending(){

        if ($this->isOnSubmit()) {
            $shops = $this->request->input('shops');
            $eventType = $this->request->input('eventType');

            if ($eventType === 'delete') {
                foreach ($shops as $shop_id) {
                    ShopAuth::where('shop_id', $shop_id)->delete();
                }
            }

            if ($eventType === 'accept') {
                foreach ($shops as $shop_id) {
                    ShopAuth::where('shop_id', $shop_id)->update(['auth_status'=>1]);
                    Shop::where('shop_id', $shop_id)->update(['auth_status'=>1]);
                }
            }

            if ($eventType === 'refuse') {
                foreach ($shops as $shop_id) {
                    ShopAuth::where('shop_id', $shop_id)->update(['auth_status'=>0]);
                    Shop::where('shop_id', $shop_id)->update(['auth_status'=>0]);
                }
            }

            return ajaxReturn();

        } else {

            $authlist = ShopAuth::with('shop.user')->whereHas('shop')->where('auth_status', '0')->paginate(20);
            $this->assign([
                'pagination'=>$authlist->render(),
                'shoplist'=>$authlist->map(function ($auth){
                    return $auth->shop;
                })
            ]);

            return $this->view('admin.shop.pending');
        }
    }

    /**
     * 店铺详情
     */
    public function detail($shop_id){
        $this->assign([
            'shop_id'=>$shop_id,
            'shop'=>Shop::find($shop_id),
            'auth'=>ShopAuth::where('shop_id', $shop_id)->first()
        ]);

        return $this->view('admin.shop.detail');
    }

    /**
     * 认证店铺
     */
    public function auth(){
        $shop_id = $this->request->input('shop_id');
        $auth_status = intval($this->request->input('auth_status'));
        $message = htmlspecialchars($this->request->input('message'));


        Shop::where('shop_id', $shop_id)->update(['auth_status'=>$auth_status]);
        ShopAuth::where('shop_id', $shop_id)->update(['auth_status'=>$auth_status, 'updated_at'=>time()]);
        return $this->messager()->message(trans('sysmessage.info save success'))->render();
    }

    public function edit(Request $request)
    {
        $shop_id = $request->input('shop_id', 0);
        $shop = Shop::find($shop_id);

        if ($this->isOnSubmit())
        {
            $newshop = $request->input('shop');
            $newauth = $request->input('auth');

            if ($newauth)
            {
                $shop->update($newshop);
            }

            if ($newauth)
            {
                $shop->auth()->update($newauth);
            }

            return $this->messager()->render();
        } else {
            return $this->view('admin.shop.edit',[
                'shop_id'=>$shop_id,
                'shop'=>$shop,
                'auth'=>$shop->auth
            ]);
        }
    }
}
